///*
// * Copyright (C) 2004, Velocityme Partnership
// * PersonActions.java
// *
// * Created on 28 September 2004, 07:46
// */
//
//package com.velocityme.www.interfaces.nodeactions;
//
//import com.velocityme.entity.PermissionBean;
//import com.velocityme.interfaces.*;
//import com.velocityme.valueobjects.*;
//import com.velocityme.www.actionforms.NodeIdActionForm;
//import com.velocityme.www.actionforms.PersonTimeActionForm;
//import com.velocityme.www.actionforms.TimeActionForm;
//import com.velocityme.www.actionforms.nodeactionforms.PersonValueActionForm;
//import com.velocityme.www.dto.IntegerString;
//import com.velocityme.www.dto.TimeDTO;
//import com.velocityme.www.interfaces.ShowNodeAction;
//import com.velocityme.www.utility.NodeUtil;
//import java.util.ArrayList;
//import java.util.Calendar;
//import java.util.Collection;
//import java.util.Date;
//import java.util.Iterator;
//import java.util.Set;
//import javax.servlet.http.*;
//import org.apache.struts.action.*;
//import org.apache.struts.actions.DispatchAction;
//
///**
// *
// * @author  Robert
// */
//public class PersonActions extends ContactableActions {
//
//    /** Creates a new instance of PersonActions */
//    public PersonActions() {
//    }
//
//    public ActionForward show(ActionMapping mapping,
//                              ActionForm form,
//                              HttpServletRequest request,
//                              HttpServletResponse response) throws Exception {
//        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
//        PersonPK personPK = new PersonPK(nodePK.getNodeId());
//        HttpSession session = request.getSession();
//        KeySession key = (KeySession)session.getAttribute("sessionKey");
//        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
//        session.setAttribute("personValue", cs.getPersonValue(key, personPK));
//        session.setAttribute("userValues", cs.getPersonUserValues(key, personPK));
//        return mapping.findForward("ACTION_DISPLAY");
//    }
//
//    public ActionForward showTodayTime(ActionMapping mapping,
//                                       ActionForm form,
//                                       HttpServletRequest request,
//                                       HttpServletResponse response) throws Exception {
//        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
//        HttpSession session = request.getSession();
//        KeySession key = (KeySession)session.getAttribute("sessionKey");
//        PersonPK personPK = key.getPersonPK();
//        PersonTimeActionForm personTimeActionForm = new PersonTimeActionForm();
//        personTimeActionForm.setNodeId(nodePK.getNodeId().intValue());
//        personTimeActionForm.setDate(new Date());
//        return showTime(mapping, personTimeActionForm, request, response);
//    }
//
//    public ActionForward showTime(ActionMapping mapping,
//                                  ActionForm form,
//                                  HttpServletRequest request,
//                                  HttpServletResponse response) throws Exception {
//        PersonTimeActionForm personTimeActionForm = (PersonTimeActionForm)form;
//        NodePK nodePK = new NodePK(new Integer(personTimeActionForm.getNodeId()));
//        HttpSession session = request.getSession();
//        KeySession key = (KeySession)session.getAttribute("sessionKey");
//        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
//        NodeValue nodeValue = cs.getNodeValue(key, nodePK);
//        ShowNodeAction.setupTreeDisplay(session, nodeValue, "Person Time");
//        PersonPK personPK = key.getPersonPK();
//        Calendar start = Calendar.getInstance();
//        Calendar stop = Calendar.getInstance();
//        start.setTime(personTimeActionForm.getDate());
//        stop.setTime(personTimeActionForm.getDate());
//        stop.add(Calendar.DATE, 1);
//        String btnSubmit = request.getParameter("btnSubmit");
//        if (btnSubmit != null) {
//            if (btnSubmit.equals("<")) {
//                start.add(Calendar.DATE, -1);
//                stop.add(Calendar.DATE, -1);
//            }
//            if (btnSubmit.equals(">")) {
//                start.add(Calendar.DATE, 1);
//                stop.add(Calendar.DATE, 1);
//            }
//        }
//        personTimeActionForm.setDate(start.getTime());
//        Iterator i = cs.getPersonTimeValues(key, personPK, start.getTime(), stop.getTime()).iterator();
//        Collection timeDTOs = new ArrayList();
//        long totalDuration = 0;
//        PermissionPK permissionTimeEdit = new PermissionPK(new Integer(PermissionBean.TIME_EDIT));
//        PermissionPK permissionTimeDelete = new PermissionPK(new Integer(PermissionBean.TIME_DELETE));
//        while(i.hasNext()) {
//            TimeValue timeValue = (TimeValue)i.next();
//            Set assignedPermissions = cs.getAssignedPermissions(key, timeValue.getTaskValue().getNodeValue().getPrimaryKey());
//            boolean isEditable = assignedPermissions.contains(permissionTimeEdit);
//            boolean isDeletable = assignedPermissions.contains(permissionTimeDelete);
//            int timeId = timeValue.getTimeId().intValue();
//            int taskId = timeValue.getTaskValue().getTaskId().intValue();
//            String taskName = timeValue.getTaskValue().getNodeValue().getName();
//            String personName = timeValue.getPersonValue().getContactableValue().getNodeValue().getName();
//            String description = timeValue.getDescription();
//            String stateName = timeValue.getStateValue().getNodeValue().getName();
//            Date startTime = timeValue.getStartTime();
//            Date stopTime = timeValue.getStopTime();
//            long duration = timeValue.getStopTime().getTime() - timeValue.getStartTime().getTime();
//            totalDuration += duration;
//            int remainingDuration_min = timeValue.getRemainingDuration_min().intValue();
//            timeDTOs.add(new TimeDTO(isEditable, isDeletable, timeId, taskId, taskName, personName, description, stateName, startTime, stopTime, duration, remainingDuration_min));
//            start.setTime(timeValue.getStopTime());
//        }
//        session.setAttribute("personTimeValues", timeDTOs);
//        session.setAttribute("personDuration", new Long(totalDuration));
//        session.setAttribute("years", NodeUtil.getInstance().getYears());
//        session.setAttribute("months", NodeUtil.getInstance().getMonths());
//        session.setAttribute("days", NodeUtil.getInstance().getDays());
//        session.setAttribute("personTimeActionForm", personTimeActionForm);
//        showCreatePersonTime(mapping, form, request, response, start.getTime());
//        return mapping.findForward("ACTION_DISPLAY");
//    }
//
//    public class PersonTimeActionForm2 {
//        private Collection m_statePairs = new ArrayList();
//        private Collection m_percentageComplete = new ArrayList();
//        private Collection m_durationDays = new ArrayList();
//        private Collection m_durationHours = new ArrayList();
//        private Collection m_durationMins = new ArrayList();
//
//        public void add(Byte percentageComplete, int totalMins, Collection statePairs) {
//            m_percentageComplete.add(percentageComplete);
//            m_durationDays.add(new Integer(totalMins / (8 * 60)));
//            m_durationHours.add(new Integer((totalMins / 60) % 8));
//            m_durationMins.add(new Integer(totalMins % 60));
//            m_statePairs.add(statePairs);
//        }
//
//        public Collection getStatePairs() { return m_statePairs; }
//
//        public Collection getPercentageCompletes() { return m_percentageComplete; }
//
//        public Collection getDurationDays() { return m_durationDays; }
//
//        public Collection getDurationHours() { return m_durationHours; }
//
//        public Collection getDurationMins() { return m_durationMins; }
//    }
//
//    public void showCreatePersonTime(ActionMapping mapping,
//                                     ActionForm form,
//                                     HttpServletRequest request,
//                                     HttpServletResponse response,
//                                     Date start)
//            throws Exception {
//        HttpSession session = request.getSession();
//        KeySession key = (KeySession) session.getAttribute("sessionKey");
//        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
//        session.setAttribute("timeValue", new TimeValue());
//        PersonTimeActionForm2 ptaf = new PersonTimeActionForm2();
//        TaskValue[] taskValues = cs.getPersonTaskValues(key);
//        Collection taskPairs = new ArrayList();
//        for (int i = 0; i < taskValues.length; i++) {
//            TaskValue taskValue = taskValues[i];
//            String pathName = cs.getProjectPathName(key, taskValue.getPrimaryKey()) + TaskValueToString.toString(taskValue);
//            taskPairs.add(new IntegerString(taskValue.getTaskId(), pathName));
//            StateValue[] stateValues = cs.getTaskStateValues(key, taskValue.getPrimaryKey());
//            Collection statePairs = new ArrayList();
//            for (int j = 0; j < stateValues.length; j++)
//                statePairs.add(new IntegerString(stateValues[j].getStateId(), stateValues[j].toString()));
//            ptaf.add(taskValue.getPercentageComplete(), taskValue.getRemainingDuration_min(), statePairs);
//            if (i == 0)
//                session.setAttribute("statePairs", statePairs);
//        }
//        Collection completeIntegerStrings = new ArrayList();
//        for (int j = 0; j <= 100; j+=5)
//            completeIntegerStrings.add(new IntegerString(new Integer(j), Integer.toString(j)));
//        session.setAttribute("taskPairs", taskPairs);
//        session.setAttribute("completePairs", completeIntegerStrings);
//        session.setAttribute("personTimeActionForm2", ptaf);
//        session.setAttribute("hours", NodeUtil.getInstance().getHours());
//        session.setAttribute("durationDays", NodeUtil.getInstance().getDurationDays());
//        session.setAttribute("durationHours", NodeUtil.getInstance().getDurationHours());
//        session.setAttribute("minutes", NodeUtil.getInstance().getMinutes());
//        // setup default values
//        TimeActionForm timeActionForm = new TimeActionForm();
//        if (taskValues.length > 0) {
//            TaskValue taskValue = taskValues[0];
//            timeActionForm.setTaskId(taskValue.getTaskId().intValue());
//            timeActionForm.setStateId(taskValue.getStateValue().getStateId().intValue());
//            timeActionForm.setPercentageComplete(taskValue.getPercentageComplete().byteValue());
//            timeActionForm.setDurationTotalMins(taskValue.getRemainingDuration_min());
//        }
//        timeActionForm.setStopDate(new Date());
//        timeActionForm.setStartDate(start); // after stop in case not today!
//        session.setAttribute("timeActionForm", timeActionForm);
//    }
//
//    public ActionForward showCreate(ActionMapping mapping,
//                                    ActionForm form,
//                                    HttpServletRequest request,
//                                    HttpServletResponse response) throws Exception {
//        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
//        PersonPK personPK = new PersonPK(nodePK.getNodeId());
//        HttpSession session = request.getSession();
//        KeySession key = (KeySession)session.getAttribute("sessionKey");
//        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
//        session.setAttribute("parentNodePK", nodePK);
//        PersonValueActionForm personValueActionForm = new PersonValueActionForm();
//        createContactDetails(key, cs, personValueActionForm);
//        session.setAttribute("personActionForm", personValueActionForm);
//        PersonValue personValue = new PersonValue();
//        personValue.setContactableValue(new ContactableValue());
//        personValue.getContactableValue().setNodeValue(new NodeValue());
//        session.setAttribute("personValue", personValue);
//        return mapping.findForward("ACTION_EDIT");
//    }
//
//    public ActionForward showEdit(ActionMapping mapping,
//                                  ActionForm form,
//                                  HttpServletRequest request,
//                                  HttpServletResponse response) throws Exception {
//        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
//        PersonPK personPK = new PersonPK(nodePK.getNodeId());
//        HttpSession session = request.getSession();
//        KeySession key = (KeySession)session.getAttribute("sessionKey");
//        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
//        PersonValue personValue = cs.getPersonValue(key, personPK);
//        session.setAttribute("personValue", personValue);
//        PersonValueActionForm personValueActionForm = new PersonValueActionForm();
//        personValueActionForm.setFirstName(personValue.getFirstName());
//        personValueActionForm.setLastName(personValue.getLastName());
//        personValueActionForm.setJobTitle(personValue.getJobTitle());
//        personValueActionForm.setDescription(personValue.getContactableValue().getNodeValue().getDescription());
//        editContactDetails(key, cs, personValue.getContactableValue(), personValueActionForm);
//        session.setAttribute("personActionForm", personValueActionForm);
//        return mapping.findForward("ACTION_EDIT");
//    }
//
//    public ActionForward edit(ActionMapping mapping,
//                              ActionForm form,
//                              HttpServletRequest request,
//                              HttpServletResponse response) throws Exception {
//        HttpSession session = request.getSession();
//        KeySession key = (KeySession)session.getAttribute("sessionKey");
//        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
//        NodePK nodePK;
//        if (request.getParameter("btnSubmit").equals("OK")) {
//            PersonValue personValue = (PersonValue)session.getAttribute("personValue");
//            PersonValueActionForm personValueActionForm = (PersonValueActionForm)form;
//            personValue.setFirstName(personValueActionForm.getFirstName());
//            personValue.setLastName(personValueActionForm.getLastName());
//            personValue.setJobTitle(personValueActionForm.getJobTitle());
//            personValue.getContactableValue().getNodeValue().setDescription(personValueActionForm.getDescription());
//            personValue.getContactableValue().setContactDetailValues(personValueActionForm.getContactDetails(cs.getAllContactDetailValues(key)));
//            PersonPK personPK;
//            if (personValue.personIdHasBeenSet()) {
//                personPK = personValue.getPrimaryKey();
//                cs.personEdit(key, personPK, personValue);
//            }
//            else {
//                ActionErrors errors = new ActionErrors();
//                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
//                personPK = cs.personCreate(key, parentNodePK, personValue);
//            }
//            nodePK = new NodePK(personPK.getPersonId());
//        }
//        // Cancel simply displays the original node again
//        else {
//            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
//        }
//        request.setAttribute("nodePK", nodePK);
//        return mapping.findForward("ACTION_SUCCESS");
//    }
//
//}
